//
//  HOPPopoverAnimation.swift
//  WB01
//
//  Created by HOP on 16/1/3.
//  Copyright © 2016年 HOP. All rights reserved.
//

import UIKit
/// 控制modal出来的动画
class HOPPopoverAnimation: NSObject, UIViewControllerAnimatedTransitioning {

    // 转场动画的时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }

    
    // 具体实现转场动画
    // 注意: 当实现这个方法后,需要我们手动将元素添加到容器视图,充分地让我们自定义转场动画
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        // 获取到modal出来的控制器的view
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
        // 将modal出来的控制器的view添加到容器视图
        transitionContext.containerView()?.addSubview(toView!)
        // 设置y方向的缩放
        toView?.transform = CGAffineTransformMakeScale(1, 0)
        // 修改锚点
        toView?.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        // 做动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            // 清空transform
            toView?.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                // 调用completeTransition告诉系统转场动画完成了
                transitionContext.completeTransition(true)
        }
    
    
    }
    
    
    
    
    
    
    
    
    
    
}
